// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#pragma once

#include <memory>
#include <vector>

#include <nebula/compute/cast.h>                      // IWYU pragma: keep
#include <nebula/compute/kernel.h>                    // IWYU pragma: keep
#include <nebula/compute/kernels/codegen_internal.h>  // IWYU pragma: keep

namespace nebula::compute::internal {

    using CastState = OptionsWrapper<CastOptions>;

    // Cast functions are _not_ registered in the FunctionRegistry, though they use
    // the same execution machinery
    class CastFunction : public ScalarFunction {
    public:
        CastFunction(std::string name, Type::type out_type_id);

        Type::type out_type_id() const { return out_type_id_; }

        const std::vector<Type::type> &in_type_ids() const { return in_type_ids_; }

        turbo::Status add_kernel(Type::type in_type_id, std::vector<InputType> in_types,
                                OutputType out_type, ArrayKernelExec exec,
                                NullHandling::type = NullHandling::INTERSECTION,
                                MemAllocation::type = MemAllocation::PREALLOCATE);

        // Note, this function toggles off memory allocation and sets the init
        // function to CastInit
        turbo::Status add_kernel(Type::type in_type_id, ScalarKernel kernel);

        turbo::Result<const Kernel *> dispatch_exact(
                const std::vector<TypeHolder> &types) const override;

    private:
        std::vector<Type::type> in_type_ids_;
        const Type::type out_type_id_;
    };

    // See kernels/scalar_cast_*.cc for these
    std::vector<std::shared_ptr<CastFunction>> GetBooleanCasts();

    std::vector<std::shared_ptr<CastFunction>> GetNumericCasts();

    std::vector<std::shared_ptr<CastFunction>> GetTemporalCasts();

    std::vector<std::shared_ptr<CastFunction>> GetBinaryLikeCasts();

    std::vector<std::shared_ptr<CastFunction>> GetNestedCasts();

    std::vector<std::shared_ptr<CastFunction>> GetDictionaryCasts();

    std::vector<std::shared_ptr<CastFunction>> GetExtensionCasts();

    TURBO_EXPORT
    turbo::Result<std::shared_ptr<CastFunction>> GetCastFunction(const DataType &to_type);

}  // namespace nebula::compute::internal
